﻿using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Service.Framework.Utils
{
    public class OperatingSystemUtil
    {
        //
        // 摘要:
        //     linux 系统命令
        //
        // 参数:
        //   command:
        public static string Bash(string command)
        {
            string text = command.Replace("\"", "\\\"");
            Process process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "/bin/bash",
                    Arguments = "-c \"" + text + "\"",
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true
                }
            };
            process.Start();
            string result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            process.Dispose();
            return result;
        }

        //
        // 摘要:
        //     windows 系统命令
        //
        // 参数:
        //   fileName:
        //
        //   args:
        public static string Cmd(string fileName, string args)
        {
            string result = string.Empty;
            ProcessStartInfo processStartInfo = new ProcessStartInfo();
            processStartInfo.FileName = fileName;
            processStartInfo.Arguments = args;
            processStartInfo.RedirectStandardOutput = true;
            using (Process process = Process.Start(processStartInfo))
            {
                result = process.StandardOutput.ReadToEnd();
            }

            return result;
        }

        //
        // 摘要:
        //     是否为 Unix 系统
        public static bool IsUnix()
        {
            return RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
        }
    }
}
